#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

struct ListNode
{
    int val;
    struct ListNode* next;
};

struct ListNode* removeElements(struct ListNode* head, int val)
{
    struct ListNode* pre = NULL;
    struct ListNode* cur = head;

    while (cur)
    {
        if (cur->val != val)
        {
            pre = cur;
            cur = cur->next;
        }
        else
        {
            //if (pre == NULL)
            //{
            //    //cur=cur->next;
            //    head = cur->next;
            //    free(cur);
            //    cur = head;

            //}
            //else
            //{
                pre->next = cur->next;
                free(cur);
                cur = pre->next;
            //}

        }
    }
    return head;
}

int main()
{
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n5 = (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->val = 7;
    n2->val = 7;
    n3->val = 7;
    n4->val = 7;
    n5->val = 7;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = NULL;

    removeElements(n1, 7);


    return 0;

}